const { default: BigNumber } = require('bignumber.js');
const { think } = require('thinkjs');
const Base = require('./base.js');

module.exports = class extends Base {
  async indexAction() {
    if (!this.isMobile) {
      return this.display('index/mobile_index.html');
    }

    if (think.isEmpty(this.get('id'))) {
      return this.display('index/404.html');
    }

    const data = await this.model('post_list')
      .where({ id: this.get('id') })
      .find();

    if (think.isEmpty(data)) {
      return this.display('index/404.html');
    }

    if (data.company) {
      data.company_num = data.company.split(',').length;
    } else {
      data.company_num = false;
    }

    // const interCount = await this.model('process')
    //   .alias('a')
    //   .join({
    //     user_list: {
    //       join: 'left',
    //       as: 'u',
    //       on: ['user_id', 'id'],
    //     },
    //   })
    //   .field('a.*, u.avatar')
    //   .where({ 'a.job_id': this.get('id'), 'u.avatar': ['!=', null] })
    //   .select();

    const companyInfo = await this.model('company_job')
      .where({ job_id: data.id })
      .select();

    const companyInfoDistinct = await this.model('company_job')
      .where({ job_id: data.id })
      .distinct('company_id')
      .alias('a')
      .join({
        company_list: {
          join: 'left',
          as: 'p',
          on: ['company_id', 'id'],
        },
      })
      .field('p.avatar, p.name, p.city, p.scale, p.id, a.company_id')
      .select();

    data.companys = companyInfoDistinct;

    if (!think.isEmpty(companyInfo)) {
      companyInfo.sort((a, b) => a.money_end - b.money_end);
      data.max = companyInfo[companyInfo.length - 1].money_end;

      const cityArr = [];
      for (const item of companyInfo) {
        let has = false;
        for (const cityItem of cityArr) {
          if (item.city_name === cityItem.city) {
            has = true;
            break;
          }
        }
        if (!has) {
          cityArr.push({
            city: item.city_name,
          });
        }
      }
      data.citys = cityArr;
    } else {
    }

    // data.desc = encodeURI(data.desc);

    // const SchemeSerivce = this.service('weixin', 'index');
    // const url = await SchemeSerivce.getWxScheme(`id=${this.get('id')}`);
    // if (url.openlink) {
    //   this.assign('openlink', url.openlink);
    // }

    this.assign('info', data);
    this.assign('id', this.get('id'));

    return this.display();
  }

  async submitAction() {
    if (think.isEmpty(this.get('id'))) {
      return this.fail(408, '找不到信息');
    }

    if (think.isEmpty(this.get('phone'))) {
      return this.fail(408, '请填写手机号');
    }

    const data = await this.model('post_list')
      .where({ id: this.get('id') })
      .find();

    if (think.isEmpty(data)) {
      return this.display('index/404.html');
    }

    const SchemeSerivce = this.service('weixin', 'index');
    const url = await SchemeSerivce.getWxScheme(
      `id=${this.get('id')}&&phone=${this.get('phone')}`
    );
    return this.success({
      id: this.get('id'),
      openlink: url.openlink,
    });
  }
};
